//
//  SXVideoRootViewController.m
//  ShiXun
//
//  Created by lanou on 15/10/26.
//  Copyright © 2015年 张琪. All rights reserved.
//

#import "SXVideoRootViewController.h"
#import "SXRequestManager.h"
#import "SXVideoMainListCollectionViewCell.h"
#import "SXDataBaseManager.h"
#import "SXVideoDetailListViewController.h"

@interface SXVideoRootViewController () <UICollectionViewDataSource, UICollectionViewDelegate>

@property (nonatomic, strong) UICollectionView *mainListCollectionView;
@property (nonatomic, strong) NSMutableArray *dataSourceArray;

@end

@implementation SXVideoRootViewController

- (UICollectionView *)mainListCollectionView
{
    if (!_mainListCollectionView) {
        
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        layout.itemSize = CGSizeMake(kScreenWidth / 2 - 7.5, kScreenWidth / 2 - 7.5);
        layout.minimumLineSpacing = 5;
        layout.minimumInteritemSpacing = 5;
        layout.sectionInset = UIEdgeInsetsMake(5, 5, 5, 5);
        layout.scrollDirection = UICollectionViewScrollDirectionVertical;
        
        self.automaticallyAdjustsScrollViewInsets = NO;
        _mainListCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 64, kScreenWidth, kScreenHeight - 113) collectionViewLayout:layout];
        _mainListCollectionView.backgroundColor = [UIColor clearColor];
        _mainListCollectionView.dataSource = self;
        _mainListCollectionView.delegate = self;
        _mainListCollectionView.showsVerticalScrollIndicator = NO;
        [_mainListCollectionView registerClass:[SXVideoMainListCollectionViewCell class] forCellWithReuseIdentifier:@"SXVideoMainListCollectionViewCell"];
    }
    return _mainListCollectionView;
}



- (NSMutableArray *)dataSourceArray
{
    if (!_dataSourceArray) {
        _dataSourceArray = [NSMutableArray array];
    }
    return _dataSourceArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self.view addSubview:self.mainListCollectionView];
    
    [SXDataBaseManager DBManage:nil dataName:@"videoRoot" manageType:(dataBaseManageTypeQueryData) getDataFromDict:^(NSData *data) {
        [self analysisData:data];
    }];
    
    [self requestData];
}

// 请求数据
- (void)requestData
{
    [SXRequestManager requestWithType:GET urlString:kVideoMainListURLString parDic:nil finish:^(NSData *data) {
        
        [SXDataBaseManager DBManage:data dataName:@"videoRoot" manageType:(dataBaseManageTypeStoreData) getDataFromDict:nil];
        [self analysisData:data];
        
    } error:^(NSError *error) {
        NSLog(@"影音主页面请求数据失败%@", error);
    }];
}

// 数据解析
- (void)analysisData:(NSData *)data
{
    NSArray *listArray = [NSJSONSerialization JSONObjectWithData:data options:(NSJSONReadingMutableContainers | NSJSONReadingMutableLeaves) error:nil];
    [self.dataSourceArray removeAllObjects];
    for (NSDictionary *viedoDict in listArray) {
        SXVideoMainListModel *model = [[SXVideoMainListModel alloc] init];
        [model setValuesForKeysWithDictionary:viedoDict];
        [self.dataSourceArray addObject:model];
    }
    [self.mainListCollectionView reloadData];
}


// items个数
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.dataSourceArray.count;
}

// cell
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    SXVideoMainListCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"SXVideoMainListCollectionViewCell" forIndexPath:indexPath];
    
    SXVideoMainListModel *model = self.dataSourceArray[indexPath.item];
    cell.configureModel = model;
    
    return cell;
}

// 点击item
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    SXVideoDetailListViewController *detailListVC = [[SXVideoDetailListViewController alloc] init];
    detailListVC.name = [(SXVideoMainListModel *)self.dataSourceArray[indexPath.row] name];
    [self.rootRightVC.navigationController pushViewController:detailListVC animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
